import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'
import store from "../store/index"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    redirect: '/user/cart',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    children: [
      {
        path: 'cart',
        name: 'cart',
        component: () => import(/* webpackChunkName: "about" */ '../components/user/Cart.vue'),
      }
    ]
    // beforeEnter: (to, from, next) => {
    //   let token = localStorage.getItem('x-auth-token')
    //   if (to.path == "/user" && !token) {
    //     store.dispatch('showToast/asychangeisShowToast', {
    //       isShow: true,
    //       msg: '先登录，要不樱姐打你',
    //       type: 'denger'
    //     })
    //     return;
    //   } else {
    //     next();
    //   }
    // }
  },
  {
    path: '/order',
    name: 'order',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName: "about" */ '../components/Error.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem('x-auth-token')
//   if (to.path == "/user" && !token) {
//     store.dispatch('showToast/asychangeisShowToast', {
//       isShow: true,
//       msg: '先登录，要不樱姐打你',
//       type: 'denger'
//     })
//     return;
//   } else {
//     next();
//   }
// })

import Router from 'vue-router'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
